﻿namespace jzgl.Controller
{
    using jzgl.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;

    public class MaterInfoDB
    {
        private SqlConnection conn = null;
        private ConnDB conndb = new ConnDB();
        private DataTable table = null;
        private SysUser user = new SysUser();

        public MaterInfoDB()
        {
            this.conn = this.conndb.GetConnString();
        }

        public int Add_MaterInfo(MaterInfo m)
        {
            int num = 0;
            try
            {
                try
                { 
                    string sql = string.Concat(new object[] { "insert into MaterInfo values('", m._MaterInfoName, "','", m._MaterUnit, "','", m._MaterPrice, "','", m._MaterTech, "','", m._MaterRemark, "','", m._MaterType, "','", m._MaterTypeName, "')" });
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            finally
            {
            }
            return num;
        }

        public int Delete_MaterInfo()
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "delete from MaterInfo";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }

        public int Delete_MaterInfo(string MaterInfoId)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "delete from MaterInfo '" + MaterInfoId + "'";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }

        public int Delete_MaterInfoBY_type_Id(int type_id)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "delete from MaterInfo where MaterType='" + type_id + "'";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }

        public DataTable Show_MaterInfo()
        {
            try
            {
                try
                {
                    string sql = "select * from MaterInfo";
                    this.table = this.conndb.FillDataTable(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return this.table;
        }

        public DataTable Show_MaterInfo(string matertype_id)
        {
            try
            {
                try
                {
                    string sql = "select * from MaterInfo   where  MaterType='" + matertype_id + "'";
                    this.table = this.conndb.FillDataTable(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return this.table;
        }

        public void Show_MaterInfo(DataGridView dgv)
        {
            try
            {
                try
                {
                    string sqlstr = "select * from MaterInfo";
                    this.conndb.dgvDataBind(dgv, sqlstr);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
        }

        public DataTable Show_MaterInfoBY_typeName(string matertype_name)
        {
            try
            {
                try
                {
                    string sql = "select * from MaterInfo   where  MaterTypeName  like'%" + matertype_name + "%'";
                    this.table = this.conndb.FillDataTable(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return this.table;
        }

        public int Update_MaterInfo(MaterInfo m)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = string.Concat(new object[] { "update MaterInfo set MaterInfoName='", m._MaterInfoName, "',MaterType='", m._MaterType, "',MaterUnit='", m._MaterUnit, "',MaterPrice='", m._MaterPrice, "',MaterTech='", m._MaterTech, "',MaterRemark='", m._MaterRemark, "'  where  MaterInfoId='", m._MaterInfoId, "'" });
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }
    }
}

